<template>
  <view class="shop-car">
    <commonCar
      v-if="resp == true"
      :list="goodsProducts"
      @delect="delect"
      @accounts="accounts"
    >
    </commonCar>
    <image src="" mode=""></image>
    <u-toast ref="notice"></u-toast>
  </view>
</template>

<script>
import commonCar from "../../components/shopCar/shopCar.vue";
export default {
  components: {
    commonCar,
  },
  onLoad() {
    this.businessinfo = this.$u.business.check();
    this.getcartpro();
  },
  data() {
    return {
      cartids: [],
      businessinfo: {},
      total:0,
      resp: false,
      goodsProducts: [],
      // goodsProducts: [{
      // 	name: '周玉赞旗舰店',
      // 	checked: 1,
      // 	goods: [{
      // 		img: 'https://cdn.uviewui.com/uview/album/1.jpg',
      // 		title: '华为荣耀',
      // 		remark: '256G',
      // 		price: '128.80',
      // 		number: 1,
      // 		checked: 1
      // 	}, {
      // 		img: 'https://cdn.uviewui.com/uview/album/1.jpg',
      // 		title: 'mate40',
      // 		remark: '128G',
      // 		price: '116.80',
      // 		number: 1,
      // 		checked: 1
      // 	}]
      // },{
      // 	name: '奢总汇旗舰店',
      // 	checked: 1,
      // 	goods: [{
      // 		img: 'https://cdn.uviewui.com/uview/album/1.jpg',
      // 		title: 'P40',
      // 		remark: '64G',
      // 		price: '129.80',
      // 		number: 1,
      // 		checked: 1
      // 	}, {
      // 		img: 'https://cdn.uviewui.com/uview/album/1.jpg',
      // 		title: 'P50',
      // 		remark: '32G',
      // 		price: '156.80',
      // 		number: 1,
      // 		checked: 1
      // 	}]
      // }],
    };
  },

  methods: {
    // 清除指定购物车的数据
    async destroycart() {
      let data = {
        busid: this.businessinfo.id,
        cartids: this.cartids,
      };
      let result = await this.$u.api.order.delete(data);
      if (result.code) {
        this.$refs.notice.show({
          type: "success",
          message: result.msg,
          duration: 800,
          complete: () => {
            uni.$u.route({
              type: "redirectTo",
              url: "pagesC/order/car",
              params: {
                busid: this.businessinfo.id,
              },
            });
          },
        });
      } else {
        this.$refs.notice.show({
          type: "error",
          message: result.msg,
          duration: 1500,
          complete: () => {
            uni.$u.route({
              type: "redirectTo",
              url: "pagesC/order/car",
              params: {
                busid: this.businessinfo.id,
              },
            });
          },
        });
      }
    },
    // 清除购物车
    async delect(e) {
      if (e.length > 0) {
        uni.showModal({
          title: "温馨提示",
          content: "请确认是否删除该购物车数据",
          cancelText: "取消",
          confirmText: "确定",
          success: (res) => {
            if (res.confirm) {
              this.cartids = e;
              this.destroycart();
            } else {
            }
          },
        });
      } else {
        uni.showToast({
          title: "您当前未选择任何商品,删除失败",
          icon: "none",
        });
      }
    },
    accounts(e) {

      this.total = e.total
     
      let cartids = Object.values(e.judge);
      this.createOrder(cartids)
      // uni.$u.route({
      //   url: "pagesC/order/orderDetail",
      //   params:e
      // });
    },

    async createOrder(item) {
      let data = {
        busid: this.businessinfo.id,
        cartids: item,
        total:this.total
      };

      let result = await this.$u.api.order.cartadds(data);
      if (result.code) {
        uni.$u.route({
          url: "pagesC/order/orderDetail",
          params: {
            orderid:result.data
          },
        });
      }
    },
    // 获取购物车全部商品
    async getcartpro() {
      let data = {
        busid: this.businessinfo.id,
      };

      let result = await this.$u.api.order.cartindex(data);
      if (result.code) {
        this.goodsProducts = result.data;
        //等到拿到数据才打开开关把数据给到子组件
        this.resp = true;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.shop-car {
  width: 750rpx;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #f5f5f5;
}
</style>
